﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SpiderHelper.Tools
{
    public class CommonTool
    {
        /// <summary>
        /// 得到没有HTML标签的网页文本
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetNoHTMLText(string str)
        {
            string html = str;

            html = Regex.Replace(html, @"<!--[\s\S]*?-->", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<img[\s\S]*?s\d{1,3}?.sinaimg[\s\S]*?>", "", RegexOptions.IgnoreCase);//过滤博客中的私密图片
            //  html = Regex.Replace(html, @"<img[\s\S]*?s\d\d.sinaimg[\s\S]*?>", "", RegexOptions.IgnoreCase);//过滤博客中的私密图片
            //  html = Regex.Replace(html, @"<img[\s\S]*?s\d\d\d.sinaimg[\s\S]*?>", "", RegexOptions.IgnoreCase);//过滤博客中的私密图片
            //html = Regex.Replace(html, @"<img[\s\S]*?s12.sinaimg[\s\S]*?>", "", RegexOptions.IgnoreCase);
            //  html = Regex.Replace(html, @"<[//]*p[^>]*>", "\n", RegexOptions.IgnoreCase);
            // html = Regex.Replace(html, @"<[//]*br[^>]*>", "</p><p>", RegexOptions.IgnoreCase);
            //html = Regex.Replace(html, @"<[//]*div[^>]*>", "\n", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<STYLE[\s\S]*?</STYLE>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"<script[\s\S]*?</script>", "", RegexOptions.IgnoreCase);

            html = Regex.Replace(html, @"<span [\s\S]*?>", "", RegexOptions.IgnoreCase);//去掉span的样式
            html = Regex.Replace(html, "</span>", "", RegexOptions.IgnoreCase);

            html = Regex.Replace(html, @"<b[\s\S]*?>", "", RegexOptions.IgnoreCase);//去掉b的样式
            html = Regex.Replace(html, "</b>", "", RegexOptions.IgnoreCase);

            html = Regex.Replace(html, @"<font [\s\S]*?>", "", RegexOptions.IgnoreCase);//去掉font的样式
            html = Regex.Replace(html, "</font>", "", RegexOptions.IgnoreCase);

            html = Regex.Replace(html, @"<p [\s\S]*?>", "<p>", RegexOptions.IgnoreCase);//去掉p的样式

            html = Regex.Replace(html, @"<u [\s\S]*?>", "", RegexOptions.IgnoreCase);//去掉u的样式
            html = Regex.Replace(html, "</u>", "", RegexOptions.IgnoreCase);

            html = Regex.Replace(html, @"<i [\s\S]*?>", "", RegexOptions.IgnoreCase);//去掉i的样式
            html = Regex.Replace(html, "</i>", "", RegexOptions.IgnoreCase);

            html = Regex.Replace(html, @"<div [\s\S]*?>", "<div>", RegexOptions.IgnoreCase);//去掉div的样式
            html = Regex.Replace(html, @"<[//]*wbr[^>]*>", "", RegexOptions.IgnoreCase);//去掉软换行符
            html = Regex.Replace(html, @"<[//]*strong[^>]*>", "", RegexOptions.IgnoreCase);//去掉strong标签
            html = Regex.Replace(html, @"<a [\s\S]*?]</a>", "", RegexOptions.IgnoreCase);//去掉微博标签

            //html = Regex.Replace(html, "<<", "<", RegexOptions.IgnoreCase);//去掉<<标签
            //html = Regex.Replace(html, ">>", ">", RegexOptions.IgnoreCase);//去掉>>标签
            // html = Regex.Replace(html, @"<[\?!A-OQ-Za-oq-z/][^><]*>", "");
         //   html = Regex.Replace(html, "\r", "</p><p>", RegexOptions.IgnoreCase);
         //   html = Regex.Replace(html, "\r\n", "</p><p>", RegexOptions.IgnoreCase);
        //    html = Regex.Replace(html, "\n\r", "", RegexOptions.IgnoreCase);
        //    html = Regex.Replace(html, "\n", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, @"[　|\s]*\r\n[　|\s]*\r\n", "", RegexOptions.IgnoreCase);
            //html = Regex.Replace(html,@"(\r\n)[^ 　]","$1");
            html = formatHtml(html);
            html = DropBr(html);
            html = DropA(html);
            return (html);
        }
        /// <summary>
        /// 去掉HTML中的Style样式
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        private static string DropBr(string html)
        {
            int brindex = html.IndexOf("<br");
            if (brindex > 0)
            {
                string forcehtml = html.Substring(0, brindex);
                int divindex = forcehtml.LastIndexOf("<div");
                int pindex = forcehtml.LastIndexOf("<p");
                if (pindex > divindex && pindex > 0)
                {
                    html = Regex.Replace(html, @"<[//]*br*[//]*>", "</p><p>", RegexOptions.IgnoreCase);
                }
                else if (divindex > pindex && divindex > 0)
                {
                    html = Regex.Replace(html, @"<[//]*br*[//]*>", "</div><div>", RegexOptions.IgnoreCase);
                }
            }


            return html;
        }
        /// <summary>
        /// 删除超链接
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        private static string DropA(string html)
        {
            html = Regex.Replace(html, @"<a [\s\S]*?>", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "</a>", "", RegexOptions.IgnoreCase);
            return html;
        }

        private static string formatHtml(string str)
        {
            //替换<P>
            string html = Regex.Replace(str, " ", " ", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&nbsp;", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&nbsp;", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, " ", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, " ", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&#8226;", " ", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&#8226", " ", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&#146;", "'", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&#147;", "“", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&#148;", "”", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&#160;", "", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&amp;", "&", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&copy;", "@", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&#150;", "–", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&quot;", " ", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&lt;", " ", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&gt;", " ", RegexOptions.IgnoreCase);
            html = Regex.Replace(html, "&#13;&#10;", "", RegexOptions.IgnoreCase);
            return html;
        }
    }
}
